package com.jiuwan.gamepass.service.impl;



import com.jiuwan.gamepass.dao.PicDao;
import com.jiuwan.gamepass.dto.appJson.ImageJson;
import com.jiuwan.gamepass.pojo.Pic;
import com.jiuwan.gamepass.service.UploadService;
import com.jiuwan.gamepass.utils.ImageSizeUtil;
import com.jiuwan.gamepass.utils.UUIDUtil;
import com.jiuwan.gamepass.utils.UploadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @version 1.0
 * 功能目的描述 ：用于  上传图片========>>>>>>>>>>>
 * @author： pang-yun
 * @date： 2021-06-04 19:29
 */


@Service
@Slf4j
public class UploadServiceImpl implements UploadService {
    private static final String SIZE = "2208*1242";
    private static final String SIZE_1 = "1242*2208";

    @Autowired
    private PicDao picDao;


    @Override
    public ImageJson uploadImage(MultipartFile[] files, String appID) {
        ImageJson imageJson = new ImageJson();

        if (files == null || files.length == 0) {
            imageJson.setCode(0);
            imageJson.setMsg("请选中要上传的文件");

            return imageJson;
        }

        imageJson.setCode(1);
        imageJson.setMsg("上传成功");

        HashMap<String, String> map = new HashMap<>();
        ArrayList<Pic> imageList = new ArrayList<>();

        int i1 = picDao.deleteByPicturesid(appID);
        log.info("删除原图片数据库{}条数据", i1);

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];


            //限制获取图片的大小
            String s = ImageSizeUtil.imageSize(file);
            System.out.println(s);
            if (s.equals(SIZE) == false & s.equals(SIZE_1) == false) {
                imageJson.setCode(0);
                imageJson.setMsg("第" + (i + 1) + "张上传失败,请输入合适尺寸");
                return imageJson;
            }
            if (i > 9) {
                picDao.deleteByPicturesid(appID);
                imageJson.setCode(0);
                imageJson.setMsg("已经超出10张的限制，请确定数目后再上传");
                return imageJson;
            }

            //调用自己写的上传文件工具类
            String imagePath = UploadUtils.upload(file);

            if (imagePath != null) {

                //将图片信息放到pic表中
                Pic pic = new Pic();

                String uuid = UUIDUtil.createUUID();

                pic.setId(uuid);
                pic.setUrl(imagePath);

                Date date = new Date();
                pic.setCreatetime(date);

                //将app的uuid放入pic_app中的appID
                pic.setPicturesid(appID);
                log.info("pic准备加入数据库+{}", pic.getId());
                picDao.insertSelective(pic);

                map.put("src" + "-" + i, imagePath);
                map.put("uuid" + "-" + i, uuid);    //uuid 是用来寻找图片的

                imageList.add(pic);


            } else {
                imageJson.setCode(0);
                imageJson.setMsg("上传失败");
            }


        }
        imageJson.setData(map);
        imageJson.setPicList(imageList);


        return imageJson;
    }
}


